package com.dingwen.treasure.file.generate.controller;

import com.dingwen.treasure.base.controller.BaseViewController;
import com.dingwen.treasure.file.generate.bo.Student;
import com.dingwen.treasure.file.generate.component.FileComponent;
import com.dingwen.treasure.file.generate.util.FreeMarkerUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 生成pdf: 模版文件本身存在未转移的字符会导致生成报错，传递自身的内容含有未转移字符不影响
 *
 * @author dingwen
 * @date 2022/03/10
 */
@Api(tags = "生成pdf")
@RestController
@Slf4j
@RequestMapping("/generate")
public class GenerateController implements BaseViewController {

    @Resource
    private FileComponent fileComponent;

    @Resource
    private FreeMarkerUtil freeMarkerUtil;


    @ApiOperation("生成pdf下载")
    @GetMapping("pdf")
    private void generatePdf(HttpServletResponse response) {
        Map<String, Object> data = new HashMap<>(4);
        data.put("title", "测试标题");
        data.put("content", "测试内容");
        Student student1 = new Student("用户名1", 1, 1);
        Student student2 = new Student("用户名2", 2, 2);
        Student student3 = new Student("用户名3", 3, 3);
        Student student4 = new Student("用户名4", 4, 4);
        List students = new ArrayList<>();
        students.add(student1);
        students.add(student2);
        students.add(student3);
        students.add(student4);
        data.put("students", students);
//        fileComponent.freeMarkerRenderFileDownloadToPdfForZip(response, data, "word.ftlh", "word", ".zip");
        fileComponent.freeMarkerRenderFileToPdf(response, data, "hello.ftlh", "word", ".pdf", false);
    }
}





